library(httr2)
library(tidyverse)

get_cistrome_regpot <- function(id = 46129){
  str_glue('https://db3.cistrome.org/cistrome/samples/{id}/regpotential') |>
  request() |>
  req_headers("Accept" = "application/json") |>
  req_throttle(capacity = 60, fill_time_s = 60) |>
  req_options(ssl_verifypeer = FALSE) |>
  req_perform() |>
  resp_body_json(simplifyVector = T) |>
  pluck('genes') |>
  as_tibble() |>
  select(sym, val) |>
  mutate(Cistrome_id = id)
}

cist_hs_chipseq <- read_tsv('00_util_scripts/ref/hg38_tranfac_QC.txt',
                            col_types = 'dcccccdddddd')

# 0826: batch0
cist_hs_chipseq_batch0 <- cist_hs_chipseq |>
  select(Cistrome_id, External_id, Factor, Ontology) |>
  mutate(id = seq_along(Cistrome_id), batch_id = seq_along(Cistrome_id) %% 11) |>
  filter(batch_id == 0) |>
  pull(Cistrome_id) |>
  map(safely(get_cistrome_regpot), .progress = T) |>
  transpose() |>
  pluck('result') |>
  compact() |>
  list_rbind() |>
  write_csv('00_util_scripts/ref/hg38_tranfac_cistromeV3_chipseq_batch0.csv')

# collect batches ----------
bb1 <- list.files('00_util_scripts/ref/', 'chipseq_hs', full.names = T)

cist_hs_chipseq_score <-
list.files('~/append-ssd/alaria2/', 'chipseq', full.names = T) |>
  c(bb1) |>
  read_csv()

cist_hs_chipseq <- cist_hs_chipseq |>
  na.omit() |>
  mutate(good_FRiP = FRiP >= .01,
         good_DHSR = PeaksUnionDHSRatio > .7,
         good_FastQC = FastQC >= 25,
         good_PBC = PBC >= .8) |>
  filter(good_FRiP | good_DHSR | good_FastQC | good_PBC)

cist_hs_chipseq_join <- cist_hs_chipseq |>
  select(Cistrome_id, External_id, Factor, Ontology) |>
  right_join(cist_hs_chipseq_score)

cist_hs_chipseq |>
  filter(External_id %in% unique(cist_hs_chipseq_score$Cistrome_id))

cist_hs_chipseq_join |>
  mutate(target = sym, score=val, .keep = 'unused') |>
  write_csv('00_util_scripts/ref/cistrome_chipseq_hsall.csv')
